package com.itqf.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.itqf.annotation.RequiresPermissionsDesc;
import com.itqf.bean.DtsAdmin;
import com.itqf.dto.AdminDto;
import com.itqf.service.DtsAdminService;
import com.itqf.util.ResponseUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * projectName: store_parent
 *
 * @author: 赵伟风
 * time: 2021/6/10 10:20
 * description: 管理员的查询！
 */

@CrossOrigin("*")
@RestController
@RequestMapping("/admin/admin")
public class DtsAdminController {

    @Autowired
    private DtsAdminService adminService;
    //?page=1&limit=20&username=sadadasd&sort=add_time&order=desc

    private Logger logger = LoggerFactory.getLogger(DtsAdminController.class);

    @RequiresPermissions("admin:admin:list")
    @RequiresPermissionsDesc(menu = {"系统管理","管理员"},button = "查询")
    @GetMapping("list")
    public Object list(AdminDto adminDto){

        logger.info("[管理展示]开始调用！传入的参数为：{}", adminDto);

        //1.分页查询
        PageInfo<DtsAdmin> pageInfo = adminService.findAdminByPage(adminDto);
        //2.结果集封装
        Map<String, Object> data  = new HashMap<>(2);
        //获取总条数
        data.put("total",pageInfo.getTotal());
        data.put("items", pageInfo.getList());

        logger.info("[管理展示]结束调用！查询的数据为：{}", data);

        return ResponseUtil.ok(data);
    }



    @RequiresPermissions("admin:admin:delete")
    @RequiresPermissionsDesc(menu = {"系统管理","管理员"},button = "删除")
    @PostMapping("delete")
    public Object delete(@RequestBody DtsAdmin dtsAdmin){


        logger.info("[管理删除]开始调用！传入的参数为：{}", JSONObject.toJSON(dtsAdmin).toString());

        int rows = adminService.removeAdminById(dtsAdmin.getId());

        Object ret = null;
        if (rows>0) {
           ret = ResponseUtil.ok();
        }else {
           ret = ResponseUtil.fail();
        }

        logger.info("[管理展示]结束调用！删除影响行数为：{}", rows);

        return ret;
    }


    @RequiresPermissions("admin:admin:update")
    @RequiresPermissionsDesc(menu = {"系统管理","管理员"},button = "修改")
    @PostMapping("update")
    public Object update(@RequestBody DtsAdmin dtsAdmin){


        logger.info("[管理修改]开始调用！传入的参数为：{}", JSONObject.toJSON(dtsAdmin).toString());

        /**
         *
         * username
         * password 123 - 密文保存到数据
         * vavatar
         * roleids
         * updatetime
         */
        int rows = adminService.updateAdminById(dtsAdmin);

        Object ret = null;
        if (rows>0) {
            ret = ResponseUtil.ok(dtsAdmin);
        }else {
            ret = ResponseUtil.fail();
        }

        logger.info("[管理修改]结束调用！修改影响行数为：{}", rows);

        return ret;
    }


    @RequiresPermissions("admin:admin:create")
    @RequiresPermissionsDesc(menu = {"系统管理","管理员"},button = "添加")
    @PostMapping("create")
    public Object create(@RequestBody DtsAdmin dtsAdmin){


        logger.info("[管理添加]开始调用！传入的参数为：{}", JSONObject.toJSON(dtsAdmin).toString());

        /**
         *
         * username
         * password 123 - 密文保存到数据
         * vavatar
         * roleids
         * updatetime
         */
        int rows = adminService.saveAdmin(dtsAdmin);

        Object ret = null;
        if (rows>0) {
            ret = ResponseUtil.ok(dtsAdmin);
        }else {
            ret = ResponseUtil.fail();
        }

        logger.info("[管理添加]结束调用！添加影响行数为：{}", rows);

        return ret;
    }


}
